package controller;

import common.MyOrder;
import common.OrderItem;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.Comment;
import pojo.User;
import service.IOrderService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {

    private Map<String, Object> responseMsg = new HashMap<>();

    @Resource
    private IOrderService orderService;

    /**
     * 新建订单
     *
     * @param orderItem 订单数据
     * @return 新建结果
     */
    @RequestMapping("/neworder")
    @ResponseBody
    public Map<String, Object> newOrder(@RequestBody OrderItem orderItem) {
        responseMsg.clear();
        User user = orderService.newOrder(orderItem);

        if (user != null) {
            responseMsg.put("success", true);
            responseMsg.put("user", user);
            responseMsg.put("message", "order success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "order failure");
        }
        return responseMsg;
    }

    @RequestMapping("/myorder")
    @ResponseBody
    public Map<String, Object> myOrder(@RequestParam Integer uid, @RequestParam String query,
                                       @RequestParam int pagenum,
                                       @RequestParam int pagesize) {
        responseMsg.clear();
        Map<String, Object> map = new HashMap<>();
        map.put("query", '%'+query+'%');
        map.put("pagenum", (pagenum - 1) * pagesize);
        map.put("pagesize", pagesize);
        map.put("userId",uid);
        map.put("sta",0);
        List<MyOrder> myOrderList = orderService.latestOrder(map);
        int total =orderService.selectTotal(map);
        if (!myOrderList.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get my order list success");
            responseMsg.put("my_order_list", myOrderList);
            responseMsg.put("total", total);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get my order list failure");
        }
        return responseMsg;
    }

    @RequestMapping("/latestorder")
    @ResponseBody
    public Map<String, Object> latestOrder(@RequestParam Integer rid, @RequestParam String query,
                                           @RequestParam int pagenum,
                                           @RequestParam int pagesize) {
        responseMsg.clear();
        Map<String, Object> map = new HashMap<>();

        map.put("query", '%'+query+'%');
        map.put("pagenum", (pagenum - 1) * pagesize);
        map.put("pagesize", pagesize);
        map.put("sta", (byte) 0);
        map.put("restaurantId",rid);
        List<MyOrder> latestOrderList = orderService.latestOrder(map);
        int total =orderService.selectTotal(map);
        if (!latestOrderList.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get latest order list success");
            responseMsg.put("latest_order_list", latestOrderList);
            responseMsg.put("total", total);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get latest order list failure");
        }
        return responseMsg;
    }

    @RequestMapping("/completed")
    @ResponseBody
    public Map<String, Object> completedOrder(@RequestParam Integer rid, @RequestParam String query,
                                              @RequestParam int pagenum,
                                              @RequestParam int pagesize) {
        responseMsg.clear();


        Map<String, Object> map = new HashMap<>();
        map.put("query", '%'+query+'%');
        map.put("pagenum", (pagenum - 1) * pagesize);
        map.put("pagesize", pagesize);
        map.put("sta", (byte) 0);
        map.put("restaurantId",rid);
        List<MyOrder> completedOrderList = orderService.completedOrder(map);

        int  total=orderService.selectCompletedTotal(map);
        if (!completedOrderList.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get completed order list success");
            responseMsg.put("completed_order_list", completedOrderList);
            responseMsg.put("total", total);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get completed order list failure");
        }
        return responseMsg;
    }

    @RequestMapping("/receive")
    @ResponseBody
    public Map<String, Object> receiveOrder(@RequestParam Integer id) {
        responseMsg.clear();
        int result = orderService.receiveOrder(id);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "receive order success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "receive order failure");
        }
        return responseMsg;
    }

    @RequestMapping("/send")
    @ResponseBody
    public Map<String, Object> orderSend(@RequestParam Integer id) {
        responseMsg.clear();
        int result = orderService.send(id);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "send success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "send failure");
        }
        return responseMsg;
    }

    @RequestMapping("/confirm")
    @ResponseBody
    public Map<String, Object> orderConfirm(@RequestParam Integer id) {
        responseMsg.clear();
        int result = orderService.confirm(id);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "order confirm success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "order confirm failure");
        }
        return responseMsg;
    }

    @RequestMapping("/comment")
    @ResponseBody
    public Map<String, Object> orderComment(@RequestBody Comment comment) {
        responseMsg.clear();
        comment.setAddTime(new Date());
        int result = orderService.comment(comment);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "comment success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "comment failure");
        }

        return responseMsg;
    }

    @RequestMapping("/mycomment")
    @ResponseBody
    public Map<String, Object> myComment(@RequestParam Integer id) {
        responseMsg.clear();
        Comment comment = orderService.myComment(id);
        if (comment != null) {
            responseMsg.put("success", true);
            responseMsg.put("message", "show this comment success");
            responseMsg.put("rate", comment.getRate());
            responseMsg.put("content", comment.getContent());
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "comment failure");
        }

        return responseMsg;
    }
}
